package com.itheima.travel.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.util.EnumSet;

//配置注解形式的web.xml
@Configuration
public class ProjectConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
   //初始化启动时


    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        //过滤器字符集
        FilterRegistration.Dynamic dynamic = servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class);
        dynamic.setInitParameter("encoding","UTF-8");
        dynamic.addMappingForUrlPatterns(null,true,"/*");

        //会话
        //过滤一起session的仓库中的数据
        FilterRegistration.Dynamic addSessionFilter = servletContext.addFilter("springSessionRepositoryFilter", DelegatingFilterProxy.class);
        //过滤会话的路径，请求类型，和错误
        addSessionFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST,DispatcherType.ERROR),true,"/*");
    }

    //项目启动时启动配置类 SpringConfig MybatisConfig
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class,MybatisConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{PlatformSpringMvcConfig.class};
    }

    //总控制器
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}
